using HKB.Common;
using System;
using System.Data;
using System.Text;
using System.Web;
namespace HKB.Web
{
	public class AnnounceBll
	{
		private static AnnounceBll _instance = null;
		private Conn conn = null;
		public static AnnounceBll instance
		{
			get
			{
				if (AnnounceBll._instance == null)
				{
					AnnounceBll._instance = new AnnounceBll(new Conn());
				}
				return AnnounceBll._instance;
			}
		}
		public AnnounceBll()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
			if (this.conn == null)
			{
				this.conn = new Conn();
			}
		}
		private void OnError(object sender, EventArgs e)
		{
			if (this.conn != null)
			{
				this.conn.CloseConn();
			}
		}
		public AnnounceBll(Conn _conn)
		{
			this.conn = _conn;
		}
		public DataTable GetAnnounces()
		{
			string sql = "SELECT * FROM HL_Announce  order by AnnounceImportant, AnnounceBegin DESC";
			return this.conn.GetDataTable(sql);
		}
		public DataTable GetAnnounces(bool Filter)
		{
			string str = "";
			if (Filter)
			{
				str = " WHERE datediff(day,GETDATE(),AnnounceEnd)>=0 AND datediff(day,GETDATE(),AnnounceBegin)<=0 ";
			}
			string sql = "SELECT * FROM HL_Announce " + str + " order by AnnounceImportant, AnnounceBegin DESC";
			return this.conn.GetDataTable(sql);
		}
		public DataTable GetAnnounceList(params int[] PageSize)
		{
			StringBuilder stringBuilder = new StringBuilder();
			int num = (PageSize.Length > 0) ? PageSize[0] : 2147483647;
			if (num < 1)
			{
				num = 2147483647;
			}
			stringBuilder.AppendFormat("SELECT TOP {0} ", num);
			stringBuilder.Append(" AnnounceID,AnnounceTitle FROM HL_Announce ");
			stringBuilder.Append(" WHERE datediff(day,GETDATE(),AnnounceEnd)>=0 AND datediff(day,GETDATE(),AnnounceBegin)<=0 ");
			stringBuilder.Append(" order by AnnounceImportant, AddTime DESC ");
			return this.conn.GetDataTable(stringBuilder.ToString());
		}
		public DataTable GetAnnounceByAnnounceID(int AnnounceID)
		{
			string commandText = "SELECT * FROM HL_Announce Where ([AnnounceID] = @AnnounceID)";
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@AnnounceID", DbType.Int32, AnnounceID);
			return this.conn.GetDataTable(commandText, new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public bool AddAnnounce(string AnnounceTitle, int AnnounceType, int AnnounceImportant, string AnnounceContent, string AnnounceOwner, DateTime AnnounceBegin, DateTime AnnounceEnd)
		{
			string commandText = "INSERT INTO [HL_Announce] ([AnnounceTitle], [AnnounceType], [AnnounceImportant], [AnnounceContent], [AnnounceOwner], [AnnounceBegin], [AnnounceEnd], [AnnounceReadCount],[AddTime]) VALUES (@AnnounceTitle, @AnnounceType, @AnnounceImportant, @AnnounceContent, @AnnounceOwner, @AnnounceBegin, @AnnounceEnd, @AnnounceReadCount,@AddTime)";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@AnnounceTitle", DbType.String, AnnounceTitle),
				this.conn.CreateParameter("@AnnounceType", DbType.Int32, AnnounceType),
				this.conn.CreateParameter("@AnnounceImportant", DbType.Int32, AnnounceImportant),
				this.conn.CreateParameter("@AnnounceContent", DbType.String, AnnounceContent),
				this.conn.CreateParameter("@AnnounceOwner", DbType.String, AnnounceOwner),
				this.conn.CreateParameter("@AnnounceBegin", DbType.Date, AnnounceBegin),
				this.conn.CreateParameter("@AnnounceEnd", DbType.Date, AnnounceEnd),
				this.conn.CreateParameter("@AnnounceReadCount", DbType.Int32, 0),
				this.conn.CreateParameter("@AddTime", DbType.DateTime, DateTime.Now)
			};
			return this.conn.Execute(commandText, parameters) >= 1;
		}
		public bool UpdateAnnounce(int AnnounceID, string AnnounceTitle, int AnnounceType, int AnnounceImportant, string AnnounceContent, string AnnounceOwner, DateTime AnnounceBegin, DateTime AnnounceEnd)
		{
			string commandText = "UPDATE [HL_Announce] SET [AnnounceTitle] = @AnnounceTitle, [AnnounceType] = @AnnounceType, [AnnounceImportant] = @AnnounceImportant, [AnnounceContent] = @AnnounceContent, [AnnounceOwner] = @AnnounceOwner, [AnnounceBegin] = @AnnounceBegin, [AnnounceEnd] = @AnnounceEnd WHERE (([AnnounceID] = @Original_AnnounceID))";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@AnnounceTitle", DbType.String, AnnounceTitle),
				this.conn.CreateParameter("@AnnounceType", DbType.Int32, AnnounceType),
				this.conn.CreateParameter("@AnnounceImportant", DbType.Int32, AnnounceImportant),
				this.conn.CreateParameter("@AnnounceContent", DbType.String, AnnounceContent),
				this.conn.CreateParameter("@AnnounceOwner", DbType.String, AnnounceOwner),
				this.conn.CreateParameter("@AnnounceBegin", DbType.Date, AnnounceBegin),
				this.conn.CreateParameter("@AnnounceEnd", DbType.Date, AnnounceEnd),
				this.conn.CreateParameter("@Original_AnnounceID", DbType.Int32, AnnounceID)
			};
			return this.conn.Execute(commandText, parameters) >= 1;
		}
		public bool UpdateAnnounceReaded(int AnnounceID)
		{
			string commandText = "UPDATE [HL_Announce] SET [AnnounceReadCount] = [AnnounceReadCount] + 1 WHERE (([AnnounceID] = @AnnounceID))";
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@AnnounceID", DbType.Int32, AnnounceID);
			return this.conn.Execute(commandText, new IDbDataParameter[]
			{
				dbDataParameter
			}) >= 1;
		}
		public bool DeleteAnnounce(int AnnounceID)
		{
			string commandText = "Delete From [HL_Announce] Where ([AnnounceID] = @AnnounceID)";
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@AnnounceID", DbType.Int32, AnnounceID);
			return this.conn.Execute(commandText, new IDbDataParameter[]
			{
				dbDataParameter
			}) >= 1;
		}
		public bool CreateAnnounceJs(int ShowNums)
		{
			string templateContent = this.ShowAnnounce(ShowNums, "js");
			string str = "/UploadFile/";
			string str2 = "announce.js";
			FileIO fileIO = new FileIO();
			return fileIO.CreateTemplateFile(templateContent, HttpContext.Current.Server.MapPath(str + str2));
		}
		public string ShowAnnounce(int ShowNums, string AnnounceType)
		{
			string text = string.Empty;
			string text2 = string.Empty;
			string text3 = string.Empty;
			DataTable announces = this.GetAnnounces();
			if (announces != null && announces.Rows.Count > 0)
			{
				text3 = "<ul>";
				if (announces.Rows.Count < ShowNums)
				{
					ShowNums = announces.Rows.Count;
				}
				Config config = new Config();
				DataRow config2 = config.config;
				string installDir = config.InstallDir;
				string str = config2["siteUrl"].ToString();
				for (int i = 0; i < ShowNums; i++)
				{
					string text4 = str + installDir + "ShowAnnounce.aspx?id=" + announces.Rows[i]["AnnounceID"].ToString();
					string text5 = announces.Rows[i]["AnnounceType"].ToString();
					if (text5 != null)
					{
						if (!(text5 == "0") && !(text5 == "2"))
						{
							if (text5 == "1")
							{
								text2 = "window.open('" + text4 + "','公告','top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');";
							}
						}
						else
						{
							string text6 = text;
							text = string.Concat(new string[]
							{
								text6,
								"<li><a href='",
								text4,
								"' target='_blank'>",
								announces.Rows[i]["AnnounceTitle"].ToString(),
								"</a></li>"
							});
						}
					}
				}
				text3 = text3 + text + "</ul>";
				text3 = "document.write(\"" + text3 + "\");";
				if (!string.IsNullOrEmpty(text2) && AnnounceType == "js")
				{
					text3 += text2;
				}
			}
			else
			{
				text3 = "document.write(\"暂无公告\");";
			}
			return text3;
		}
	}
}
